package aod.android.com.bernay.mine.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.google.gson.Gson;
import java.io.IOException;
import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.mine.module.MineUserInfo;
import aod.android.com.bernay.util.SharePreUtils;
import chen.lion.hilib.view.bind.HiView;

/**
 * @author crated by zhixianuanyang on 2019/4/19 16:32
 * @Email jamesKing0812@gmail.com
 */
public class ModifyPersonalActivity extends BernayBaseActivity {

    @HiView(R.id.et_userInfo)
    private EditText mUserInfo;

    private Bundle bundle;
    private String title;
    private String userId;
    private String token;
    private String inputText;

    @Override
    protected int initLayout() {
        return R.layout.activity_modify_personal;
    }

    @Override
    protected void initData() {
        bundle = getIntent().getExtras();
        userId = SharePreUtils.newInstance(ModifyPersonalActivity.this).getUserId();
        token = SharePreUtils.newInstance(ModifyPersonalActivity.this).getToken();

        title = bundle.getString("title");
        if (title.indexOf("昵称") != -1){
            mUserInfo.setHint("请输入昵称");
        }else{
            mUserInfo.setHint("请输入真实姓名");
        }

        initTitle(title, getString(R.string.feedback_submit), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputText = mUserInfo.getText().toString();
                if ("".equals(inputText) && title.indexOf("昵称") != -1){
                    showToast("昵称不能为空");
                }else if ("".equals(inputText) && title.indexOf("真实姓名") != -1){
                    showToast("真实姓名不能为空");
                }else{
                    if (!"".equals(inputText) && title.indexOf("昵称") != -1){
                        modifyNickName(inputText);
                    }else {
                        modifyRealName(inputText);
                    }
                }
            }
        });
    }

    /**
     * 修改昵称
     * @param modifyStr
     */
    public void modifyNickName(String modifyStr){
        BernayApi.http().saveUserNickName(userId, token,modifyStr, new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                MineUserInfo userInfo = new Gson().fromJson(json, MineUserInfo.class);
                if (1 == userInfo.getStatus()){
                    finish();
                }else{
                    showToast("昵称修改失败");
                }
            }

            @Override
            public void onHttpFailure(IOException e) {

            }
        });
    }

    /**
     * 修改真实姓名
     * @param modifyStr
     */
    public void modifyRealName(String modifyStr){
        BernayApi.http().saveUserRealName(userId, token,modifyStr, new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                MineUserInfo userInfo = new Gson().fromJson(json, MineUserInfo.class);
                if (1 == userInfo.getStatus()){
                    finish();
                }else{

                }
            }

            @Override
            public void onHttpFailure(IOException e) {
                showToast("真实姓名修改失败");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
